package org.study.mongo.demo;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 插入数据
 * @author fangyu
 * @version v1.0.0
 * @date 2020/10/13 6:51 下午
 */
public class MongoDemo3 {
    public static void main(String[]args){
        // 创建连接
        MongoClient client = new MongoClient("10.211.55.6");
        // 打开数据库
        MongoDatabase spitdb = client.getDatabase("spitdb");
        // 获取集合
        MongoCollection<Document> spit = spitdb.getCollection("spit");

        Map<String, Object> map = new HashMap<>();
        map.put("content", "我要吐槽");
        map.put("userid","9999");
        map.put("visits",123);
        map.put("publishtime",new Date());
        Document document = new Document(map);

        // 插入一条数据
        spit.insertOne(document);
        // 关闭连接
        client.close();
    }
}
